package bai7.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Scanner;

import bai7.model.Project;
import bai7.service.implement.ProjectAPI;

public class ProjectDAO implements ProjectAPI {
	Scanner ip=new Scanner(System.in);
	ArrayList<Project> list=new ArrayList<Project>();

	public void inputProject() {
		System.out.println("Nhap so Project can them: ");
		int n=Integer.parseInt(ip.nextLine());
		for(int i=1;i<=n;i++){
			System.out.println("Nhap Project thu "+i);
			System.out.println("Nhap id: ");
			int id=Integer.parseInt(ip.nextLine());
			System.out.println("Nhap Name: ");
			String name=ip.nextLine();
			if(name.trim().equalsIgnoreCase("")){
				System.out.println("Name chua duoc nhap");
				name=ip.nextLine();
			}
			System.out.println("Nhap ngay bat dau: ");
			String startDate=ip.nextLine();
			if(startDate.trim().equalsIgnoreCase("")){
				System.out.println("Ngay chua duoc nhap");
				startDate=ip.nextLine();
			}
			if(!checkDate(startDate)){
				System.out.println("Ngay nhap vao khong dung dinh dang");
				startDate=ip.nextLine();
			}
			System.out.println("Nhap ngay ket thuc: ");
			String endDate=ip.nextLine();
			if(endDate.trim().equalsIgnoreCase("")){
				System.out.println("Ngay chua duoc nhap");
				endDate=ip.nextLine();
			}
			if(!checkDate(endDate)){
				System.out.println("Ngay nhap vao khong dung dinh dang");
				endDate=ip.nextLine();
			}
			System.out.println("Nhap mo ta");
			String description=ip.nextLine();
			if(description.trim().equalsIgnoreCase("")){
				System.out.println("Chua nhap mo ta");
				description=ip.nextLine();
			}
			Project pj=new Project(id,name,startDate,endDate,description);
			list.add(pj);
		}
	}
	public void display() {
		System.out.println("=====================Danh sach Project=======================\n");
		System.out.println("|id\t|Name\t\t|StartDate\t|EndDate\t|DEscription\n");
		for(Project pj:list){
			System.out.println("|"+pj.getId()+"\t|"+pj.getName()+"\t\t|"+pj.getStartDate()+"\t|"+pj.getEndDate()+"\t|"+pj.getDescription());
			System.out.println("-------------------------------------------------------------------------");
		}	
	}
	public void deleteProject(int id) {
		list.remove(id-1);
	}
	public void searchProject(int id) {
		for(Project pj:list){
			if(pj.getId()==id){
				System.out.println("|id\t|Name\t\t|StartDate\t|EndDate\t|DEscription\n");
				System.out.println("|"+pj.getId()+"\t|"+pj.getName()+"\t\t|"+pj.getStartDate()+"\t|"+pj.getEndDate()+"\t|"+pj.getDescription());
			}
			else{
				System.out.println("Khong tim thay id ");
			}
		}	
	}
	public boolean checkDate(String date) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
		try {
			dateFormat.setLenient(false);
			dateFormat.parse(date);
			return true;
		} catch (Exception e) {
			return false;
		}
	}
}
